<template>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <router-link
                class="navbar-brand"
                active-class="active"
                :to="{ name: 'pk' }"
            >
                King of Bots
            </router-link>
            <button
                class="navbar-toggler"
                type="button"
                data-bs-toggle="collapse"
                data-bs-target="#navbarText"
                aria-controls="navbarText"
                aria-expanded="false"
                aria-label="Toggle navigation"
            >
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarText">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <router-link
                            class="nav-link"
                            active-class="active"
                            :to="{ name: 'pk' }"
                        >
                            对战
                        </router-link>
                    </li>
                    <li class="nav-item">
                        <router-link
                            class="nav-link"
                            active-class="active"
                            :to="{ name: 'record' }"
                        >
                            对局列表
                        </router-link>
                    </li>
                    <li class="nav-item">
                        <router-link
                            class="nav-link"
                            active-class="active"
                            :to="{ name: 'ranklist' }"
                        >
                            排行榜
                        </router-link>
                    </li>
                </ul>
                <ul class="navbar-nav" v-if="$store.state.user.is_login">
                    <li class="nav-item dropdown">
                        <router-link
                            class="nav-link dropdown-toggle"
                            href="#"
                            role="button"
                            data-bs-toggle="dropdown"
                            aria-expanded="false"
                        >
                            {{$store.state.user.username}}
                        </router-link>
                        <ul class="dropdown-menu">
                            <li>
                                <router-link
                                    class="dropdown-item"
                                    :to="{ name: 'mybots' }"
                                >
                                    我的Bots
                                </router-link>
                            </li>
                            <li>
                                <hr class="dropdown-divider"/>
                            </li>
                            <li>
                                <router-link
                                    class="dropdown-item"
                                    :to="{name:'pk'}"
                                    @click="logout"
                                >
                                    退出
                                </router-link>
                            </li>
                        </ul>
                    </li>
                </ul>

                <ul class="navbar-nav" v-else>
                    <li class="nav-item">
                        <router-link
                            class="nav-link"
                            :to="{name:'login'}"
                            role="button"
                        >
                            登录
                        </router-link>
                    </li>
                    <li class="nav-item">
                        <router-link
                            class="nav-link"
                            :to="{name:'register'}"
                            role="button"
                        >
                            注册
                        </router-link>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</template>

<script>
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap/dist/js/bootstrap.js";
import {useStore} from "vuex";
export default {
    name: "NavBar",
    setup(){
        const store=useStore();
        const logout=()=>{
            store.dispatch("logout");
            localStorage.clear();
        }

        return{
            logout,
        }
    }
};
</script>

<style scoped>

</style>
